package a66weding.com.jiehuntong.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

import a66weding.com.jiehuntong.beans.DayBean;
import a66weding.com.jiehuntong.common.utils.FileUtil;

/**
 * Created by Administrator on 2016/7/5.
 */
public class HolidayDbDao {
    private Context context;
    private  File savePath;

    public HolidayDbDao(Context context){
        this.context=context;
        //获得数据库地址
        savePath = new File(FileUtil.getDbFile(context), "hdjr.db");

    }

    public DayBean getHoliday(String [] days){
        long start = System.currentTimeMillis();
        DayBean dayInfo=new DayBean();
        SQLiteDatabase db = SQLiteDatabase.openDatabase(savePath.getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY);
        String sql="select * from holiday where year=? and month=? and  day=?";
        Cursor cursor = db.rawQuery(sql,days );
        if(cursor!=null && cursor.getCount()>0) {
            while(cursor.moveToNext()){
                dayInfo.setCando(cursor.getString(cursor.getColumnIndex("yi")));
                dayInfo.setCannotDo(cursor.getString(cursor.getColumnIndex("ji")));
                dayInfo.setFlag(cursor.getInt(cursor.getColumnIndex("flag")));
            }
        }

        cursor.close();
        db.close();
        long end = System.currentTimeMillis();
        return dayInfo;
    }

    public List getMonthHoliday(int year,int month){
        long start = System.currentTimeMillis();
        Log.e("Log", "HolidayDbDao getMonthHoliday: year="+year+" month"+month);
        List<String> holidayList=new ArrayList<String>();
        DayBean dayInfo=new DayBean();
        SQLiteDatabase db = SQLiteDatabase.openDatabase(savePath.getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY);
        String sql="select day from holiday where year=? and month=? and  flag=1";
        Cursor cursor = db.query("holiday", new String[]{"day"}, "year=? and month=? and  flag=?", new String[]{String.valueOf(year), String.valueOf(month),"1"}, null, null, null);
        if(cursor!=null && cursor.getCount()>0) {
            while(cursor.moveToNext()){
                cursor.getCount();
                holidayList.add(cursor.getString(0));
            }
        }
        cursor.close();
        db.close();
        long end = System.currentTimeMillis();
        return holidayList;
    }
}
